package com.animee.tally.util;
import java.math.BigDecimal;
import java.text.DecimalFormat;

public class FloatUtil {

    public static float sub(float v1,float v2){
        BigDecimal b1 = new BigDecimal(Float.toString(v1));
        BigDecimal b2 = new BigDecimal(Float.toString(v2));
        return b1.subtract(b2).floatValue();
    }
    // 保留4位小数的除法运算
    public static float div(float v1,float v2){
        float v3 = v1/v2;
        BigDecimal b1 = new BigDecimal(Float.toString(v3));
        float val = b1.setScale(4, 4).floatValue();
        return val;
    }

    // 将浮点数类型，转换成百分比类型，保留两位小数的方法
    public static String percentage(float v1){
        float v = v1*100;
        BigDecimal b1 = new BigDecimal(Float.toString(v));
        float val = b1.setScale(2, 4).floatValue();
        String per = val+"%";
        return per;
    }

    /**
     * 给float类型保留两位小数的方法
     * v:传入的数值  scale ：保留位数
     */
    public static float saveScale(float v,int scale){
        BigDecimal b1 = new BigDecimal(Float.toString(v));
        float val = b1.setScale(scale, 4).floatValue();
        return val;
    }
}
